package cn.sinata.wanglin.net.model.util;

import android.text.TextUtils;

import com.google.gson.JsonSyntaxException;
import com.xilada.xldutils.activitys.BaseActivity;
import com.xilada.xldutils.activitys.RecyclerListActivity;
import com.xilada.xldutils.fragment.BaseLazyFragment;
import com.xilada.xldutils.fragment.RecyclerViewFragment;
import com.xilada.xldutils.utils.Toast;

import java.lang.ref.WeakReference;
import java.net.ConnectException;
import java.net.SocketTimeoutException;

import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.ResultData;

/**
 *
 */
public abstract class ResultDataSubscriber<T> extends ResultSubscriber<ResultData<T>> {
    private WeakReference<BaseActivity> activities;
    private WeakReference<BaseLazyFragment> fragments;

    public ResultDataSubscriber(BaseActivity activity) {
        super(activity);
        activities = new WeakReference<>(activity);
    }

    public ResultDataSubscriber(BaseLazyFragment fragment) {
        super(fragment);
        fragments = new WeakReference<>(fragment);
    }

    @Override
    public void onNext(ResultData<T> tResultData) {
        super.onNext(tResultData);
        dismissDialog();
        tResultData.getDecodeDesString();
        if (TextUtils.equals(tResultData.getCode(), "SUCCESS")) {
            onSuccess(tResultData.getMessage(), tResultData.getData());
        } else {
            onError(new ResultException(tResultData.getCode(), tResultData.getMessage()));
        }
    }

    @Override
    public void onError(Throwable e) {
        super.onError(e);
        dismissDialog();

        String code = null;
        String msg = HttpManager.REQUEST_ERROR;
        if (e instanceof JsonSyntaxException) {
            msg = HttpManager.PARSER_ERROR;
        } else if (e instanceof ConnectException) {
            msg = HttpManager.NET_ERROR;
        } else if (e instanceof SocketTimeoutException) {
            msg = HttpManager.NET_ERROR;
        } else if (e instanceof ResultException) {
            code = ((ResultException) e).getCode();
            msg = e.getMessage();
        }
        onError(code, msg+e.getMessage());

    }

    protected void onError(String code, String msg) {
        if (shouldShowErrorToast()) {
            showToast(msg);
        }
    }

    public abstract void onSuccess(String msg, T t);

    private void showToast(String msg) {
        if (activities != null) {
            BaseActivity activity = activities.get();
            if (activity != null) {
                Toast.create(activity).show(msg);
            }
        }
        if (fragments != null) {
            BaseLazyFragment fragment = fragments.get();
            if (fragment != null) {
                Toast.create(fragment).show(msg);
            }
        }

    }

    private void dismissDialog() {
        if (activities != null) {
            BaseActivity activity = activities.get();
            if (activity != null) {
                activity.dismissDialog();
                if (activity instanceof RecyclerListActivity) {
                    ((RecyclerListActivity) activity).setRefreshing(false);
                }
            }
        }
        if (fragments != null) {
            BaseLazyFragment fragment = fragments.get();
            if (fragment != null) {
                fragment.dismissDialog();
                if (fragment instanceof RecyclerViewFragment) {
                    ((RecyclerViewFragment) fragment).setRefreshing(false);
                }
            }
        }
    }

    protected boolean shouldShowErrorToast() {
        return true;
    }
}
